#include "view.h"
#include "ui_view.h"
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include "changepassword.h"

View::View(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::View)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::Window);   //设置无边框窗口

    //遮罩窗口突出显示弹窗
    mpShadeWindow = new QWidget(parent);
    QString str("QWidget{background-color:rgba(51,51,51,0.4);}");
    mpShadeWindow->setStyleSheet(str);
    mpShadeWindow->setGeometry(0, 0, 1, 1);
    mpShadeWindow->hide();

    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);

    //阴影颜色
    shadow_effect->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect->setBlurRadius(10);
    ui->label->setGraphicsEffect(shadow_effect);

    //部件事件过滤的注册
    ui->pushButton_2->installEventFilter(this);
    ui->pushButton_3->installEventFilter(this);

    QFont font;
    font.setLetterSpacing(QFont::PercentageSpacing,120);//120字间距，默认100
    ui->pushButton->setFont(font);
    ui->pushButton_2->setFont(font);
    ui->pushButton_3->setFont(font);

}

View::~View()
{
    delete ui;
}

//显示登入用户信息
void View::setview(QString name)
{
    ui->pushButton->setText(name);
}

bool View::eventFilter(QObject *watched, QEvent *event)
{
    if(ui->pushButton_2==watched)
    {
        if(QEvent::Enter==event->type())
        {
            ui->pushButton_2->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-style: none;");
            ui->pushButton_3->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-bottom-left-radius :16px;border-bottom-right-radius :16px;background:rgba(255, 255, 255, 0%);");

        }
        else if(QEvent::Leave==event->type())
        {
//            ui->pushButton_2->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-style: none;background:rgba(255, 255, 255, 0%);");
        }
    }

    if(ui->pushButton_3==watched)
    {
        if(QEvent::Enter==event->type())
        {
            ui->pushButton_3->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-bottom-left-radius :16px;border-bottom-right-radius :16px;");
            ui->pushButton_2->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-style: none;background:rgba(255, 255, 255, 0%);");

        }
        else if(QEvent::Leave==event->type())
        {
//            ui->pushButton_3->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-bottom-left-radius :16px;border-bottom-right-radius :16px;background:rgba(255, 255, 255, 0%);");
        }
    }

    return QObject::eventFilter(watched,event);
}

void View::receChangePassword(QString data)
{
//    qDebug()<<__LINE__<<data;
    if(data == "1")
    {
        closeShadeWindow();
    }
}

//关闭窗口
void View::on_pushButton_clicked()
{
    this->hide();
    ui->pushButton_2->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-style: none;");
    ui->pushButton_3->setStyleSheet("background: #C0E3F8;font-size: 16px;font-family: Microsoft YaHei;color: #333333;border-bottom-left-radius :16px;border-bottom-right-radius :16px;background:rgba(255, 255, 255, 0%);");

}

//重写退出事件
bool View::event(QEvent *event)
{
    if (event->type() == QEvent::ActivationChange)
    {
        if(QApplication::activeWindow() != this)
        {
            on_pushButton_clicked();
        }
    }
    return QWidget::event(event);
}

void View::showShadeWindow()
{
    QWidget *parentWidget=this->parentWidget();
    mpShadeWindow->setGeometry(0, 0, parentWidget->width(), parentWidget->height());
    mpShadeWindow->show();
}

void View::closeShadeWindow()
{
    mpShadeWindow->hide();
    mpShadeWindow->setGeometry(0,0,1,1);
}

//点击修改密码按钮
void View::on_pushButton_2_clicked()
{
    qDebug()<<__LINE__<<"点击修改密码按钮";

    emit sendView("1");

    showShadeWindow();

    cpd = new ChangePassword();
    connect(cpd, SIGNAL(sendChangePassword(QString)), this, SLOT(receChangePassword(QString)));
    cpd->show();
    on_pushButton_clicked();
}

//点击退出登入按钮
void View::on_pushButton_3_clicked()
{
    qDebug()<<__LINE__<<"点击退出登入按钮";
    this->hide();

    emit sendView("2");

}
